﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SalePartial.Utility.WebComponents;


public partial class news : System.Web.UI.Page
{
    //private Epteks.BLL.Information infoBll = new Epteks.BLL.Information();
    private DateTime expireTime = DateTime.Now.AddMinutes(1);
    private PagedDataSource pagerScr = new PagedDataSource();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                this.ViewState["typeParm"] = "0";
                this.ViewState["dateParm"] = this.ViewState["keyWordParm"] = "";

                if (Request.QueryString["type"] != null)
                {
                    this.ViewState["typeParm"] = Request["type"];
                }
                if (Request.QueryString["date"] != null)
                {
                    this.ViewState["dateParm"] = CleanString.InputText(Request["date"], 50);
                }
                if (Request.QueryString["keyWord"] != null)
                {
                    this.ViewState["keyWordParm"] = CleanString.InputText(Request["keyWord"], 50);
                }

                bindImgNews();
                bind(this.ViewState["typeParm"].ToString(), this.ViewState["keyWordParm"].ToString(), this.ViewState["dateParm"].ToString());
            }
            catch { }
        }
    }

    private void bindImgNews()
    {
        try
        {
            string cacheKey = "searchNewsImage";
            if (Cache[cacheKey] != null)
            {
                this.imgNews.DataSource = Cache[cacheKey];
            }
            else
            {
                //delete by jiang 20120929
                //IList list = infoBll.GetImageInfoByPTypeId(1);//表示父类型为新闻
                //Cache.Add(cacheKey, list, null, expireTime, TimeSpan.Zero, CacheItemPriority.High, null);
                //this.imgNews.DataSource = list;
            }

            this.imgNews.DataBind();
        }
        catch
        {/*重定向到出错页面*/}
    }

    private void bind(string typeStr, string keyWord, string dateType)
    {
        int type = 0, pTypeId = 1;    //表示父类型为新闻
        string cacheKey = "";
        IList list = null;

        try
        {
            type = Convert.ToInt32(typeStr);

            if (type != 0) //按照子类型检索
            {
                cacheKey = "searchNews" + type + "datetype" + dateType + "title" + keyWord;
                if (Cache[cacheKey] != null)
                {
                    list = (IList)Cache[cacheKey];
                }
                else
                {
                    //delete by jiang 20120929
                    //list = infoBll.GetInfoListByTypeIdAndDateTitle(type, keyWord, "", GetSearchDate(dateType));
                    //Cache.Add(cacheKey, list, null, expireTime, TimeSpan.Zero, CacheItemPriority.High, null);
                }

                //string typeName = infoBll.GetTypeNameByTypeId(type);
                //if (typeName.Length > 0)
                //{
                //    this.searchInfo.Text = "--" + typeName;
                //}
            }
            else //检索所有子类型
            {
                cacheKey = "searchNewsAll" + "datetype" + dateType + "title" + keyWord;
                if (Cache[cacheKey] != null)
                {
                    list = (IList)Cache[cacheKey];
                }
                else
                {
                    //list = infoBll.GetInfoListByParentTypeIdAndDateTitle(pTypeId, keyWord, "", GetSearchDate(dateType));
                    //Cache.Add(cacheKey, list, null, expireTime, TimeSpan.Zero, CacheItemPriority.High, null);
                }
            }

            if (keyWord.Length > 0)
            {
                this.searchInfo.Text = this.searchInfo.Text + "  关键字：" + keyWord;
            }
        }
        catch
        {
            //重定向到出错页面
        }

        pagerScr.DataSource = list;
        this.AspNetPager.RecordCount = list.Count;
        pagerScr.AllowPaging = true;
        pagerScr.CurrentPageIndex = AspNetPager.CurrentPageIndex - 1;
        pagerScr.PageSize = AspNetPager.PageSize;

        //bind数据
        this.newsList.DataSource = pagerScr;
        this.newsList.DataBind();
    }

    private DateTime GetSearchDate(string dateType)
    {
        DateTime searchDate = DateTime.Now;

        if (dateType.Equals("week"))
        {
            searchDate -= new TimeSpan(7, 0, 0, 0);
        }
        else if (dateType.Equals("month"))
        {
            searchDate -= new TimeSpan(7 * 30, 0, 0, 0);
        }
        else if (dateType.Equals("half"))
        {
            searchDate -= new TimeSpan(7 * 30 * 7, 0, 0, 0);
        }
        else //all
        {
            searchDate = new DateTime(2000,12,12);
        }

        return searchDate;
    }

    protected void AspNetPager_PageChanged(object src, EventArgs e)
    {
        bind(this.ViewState["typeParm"].ToString(), this.ViewState["dateParm"].ToString(), this.ViewState["keyWordParm"].ToString());
    }
}
